package com.tma.mobilechart;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.tma.mobilechart.utilitys.ColorPickerDialog;
import com.tma.mobilechart.utilitys.ColorPickerDialog.OnColorChangedListener;

public class PieChartActivity extends Activity implements OnClickListener,
		ColorPickerDialog.OnColorChangedListener {
	EditText etPieChartTitle, etPieChartDescription;
	CheckBox chbPieChartDisplayValue;
	CheckBox chbPieChart3d;
	private Button btnPieChartView, btnPieChartSave, btnPieChartCancel;

	private Button btnPieChartTitleColor, btnPieChartContentColor,
			btnPieChartValueColor, btnPieChartGroupColor;

	private LinearLayout linearInputData;
	private LayoutInflater lInflater;
	private int pieTitleColor, pieContentColor, pieValueColor, pieGroupColor;
	private OnColorChangedListener pieColorChanged;

	private int colorState = 0;
	EditText etXValue, etYValue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.layout_editer_piechart);

		// Set default color
		pieTitleColor = 0xFF51A9FF;
		pieContentColor = 0xFF51A9FF;
		pieValueColor = 0xFF51A9FF;

		pieColorChanged = this;

		// etPieChartTitle = (EditText)findViewById(R.id.et_piechart_title);
		// etPieChartDescription =
		// (EditText)findViewById(R.id.et_piechart_description);
		chbPieChartDisplayValue = (CheckBox) findViewById(R.id.chb_piechart_displayvalue);
		chbPieChart3d = (CheckBox) findViewById(R.id.chb_piechart_3d);

		btnPieChartView = (Button) findViewById(R.id.button_piechart_view);
		btnPieChartSave = (Button) findViewById(R.id.button_piechart_save);
		btnPieChartCancel = (Button) findViewById(R.id.button_piechart_cancel);

/*		btnPieChartTitleColor = (Button) findViewById(R.id.button_piechart_color_title);
		btnPieChartContentColor = (Button) findViewById(R.id.button_piechart_color_content);
		btnPieChartValueColor = (Button) findViewById(R.id.button_piechart_color_value);*/
		btnPieChartGroupColor = (Button) findViewById(R.id.bt_piechart_group_color_12);

		// Layout Inflater
		linearInputData = (LinearLayout) findViewById(R.id.linear_input_data);
		lInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// Add listener
		btnPieChartView.setOnClickListener(this);
		btnPieChartSave.setOnClickListener(this);
		btnPieChartCancel.setOnClickListener(this);

/*		btnPieChartTitleColor.setOnClickListener(this);
		btnPieChartContentColor.setOnClickListener(this);
		btnPieChartValueColor.setOnClickListener(this);*/

		/*
		 * btnAdd.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) { InputData item=new
		 * InputData(etXValue.getText().toString(),
		 * etYValue.getText().toString()); listData.add(item);
		 * mAdapter.notifyDataSetChanged(); } });
		 */
	}

	@Override
	public void colorChanged(int color) {
		// colorState = 0 -> Title Color
		// colorState = 1 -> Content Color
		// colorState = 2 -> Value Color

		/*switch (colorState) {
		case 0:
			pieTitleColor = color;
			btnPieChartTitleColor.setBackgroundColor(pieTitleColor);
			break;
		case 1:
			pieContentColor = color;
			btnPieChartContentColor.setBackgroundColor(pieContentColor);
			break;
		case 2:
			pieValueColor = color;
			btnPieChartValueColor.setBackgroundColor(pieValueColor);
			break;
		default:
			break;
		}*/
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button_piechart_view:
			// event
			if (chbPieChartDisplayValue.isChecked()) {
				Toast.makeText(getBaseContext(), "Display value", 200).show();
			} else {
				Toast.makeText(getBaseContext(), "None display value", 200)
						.show();
			}

			if (chbPieChart3d.isChecked()) {
				Toast.makeText(getBaseContext(), "3d", 200).show();
			} else {
				Toast.makeText(getBaseContext(), "2d", 200).show();
			}

			// start render
			Intent pieIntent = new Intent(v.getContext(),
					RenderLineActivity.class);
			startActivityForResult(pieIntent, 0);
			break;
		case R.id.button_piechart_save:
			Toast.makeText(v.getContext(), "Button Save", 200).show();
			break;
		case R.id.button_piechart_cancel:
			finish();
			break;
		/*case R.id.button_piechart_color_title:
			colorState = 0;
			new ColorPickerDialog(v.getContext(), pieColorChanged,
					pieTitleColor).show();
			break;
		case R.id.button_piechart_color_content:
			colorState = 1;
			new ColorPickerDialog(v.getContext(), pieColorChanged,
					pieContentColor).show();
			break;
		case R.id.button_piechart_color_value:
			colorState = 2;
			new ColorPickerDialog(v.getContext(), pieColorChanged,
					pieValueColor).show();
			break;*/
		default:
			break;
		}
	}

	// Dynamic Items and Groups methods, onClick events are declared in XML
	public void addItem(View v) {
		View myView = lInflater.inflate(R.layout.dynamic_item_piechart, null);
		linearInputData.addView(myView);
	}

	public void deleteItem(View v) {
		linearInputData.removeView((View) v.getParent());
	}
	
	public void setCatColor(final View v) {
		int initialColor = Color.parseColor(v.getTag().toString()
				.replace("0x", "#"));
		new ColorPickerDialog(v.getContext(), new OnColorChangedListener() {

			@Override
			public void colorChanged(int color) {
				// TODO Auto-generated method stub
				v.setBackgroundColor(color);
				String tag = "0x" + Integer.toHexString(color);
				v.setTag(tag);
			}
		}, initialColor).show();
	}
}
